package org.ediba.siri.web;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;

import org.apache.commons.beanutils.BeanMap;
import org.ediba.siri.core.image.domain.Image;
import org.ediba.siri.core.image.service.ImageService;
import org.ediba.siri.core.request.domain.Request;
import org.ediba.siri.core.request.domain.RequestAttachment;
import org.ediba.siri.core.request.service.RequestService;
import org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView;

public class RequestsJasperReportsPdfView extends JasperReportsPdfView {
		
	private ImageService imageService;
	public void setImageService(ImageService imageService){this.imageService = imageService;}

	private RequestService requestService;
	public void setRequestService(RequestService requestService){this.requestService = requestService;}
	
	@Override
	protected JRDataSource convertReportData(Object value) throws IllegalArgumentException {
		
		@SuppressWarnings("unchecked")
		Collection<Request> _requests = (Collection<Request>) value;
		Collection<Map<String,Object>> _requestsMapCollection = new ArrayList<Map<String,Object>>(_requests.size());
		Collection<Map<String,Object>> _renderableImages;
		BeanMap _imageBeanMap;
		Map<String,Object> _imageDynMap;
		BeanMap _requestBeanMap;
		Map<String,Object> _requestDynMap;
		Map<String,InputStream> _thumb;
		InputStream _inputStream = null;
		
		for (Request _request : _requests) {
			
			_requestBeanMap = new BeanMap(_request);
			_requestDynMap = new HashMap<String, Object>(_requestBeanMap.size() + 2);
			
			_renderableImages = new ArrayList<Map<String,Object>>(_request.getImages().size());
			for (Image _image : _request.getImages()) {
				_thumb = this.imageService.retrieveThumb(_image.getId());
				if (_thumb != null)
					for (Map.Entry<String,InputStream> _thumbEntry : _thumb.entrySet())
						_inputStream = _thumbEntry.getValue();
				else 
					try {_inputStream = new FileInputStream(this.getServletContext().getRealPath("/images/broken-image.png"));}
					catch (FileNotFoundException e) {e.printStackTrace();}
					
				_imageBeanMap = new BeanMap(_image);
				_imageDynMap = new HashMap<String, Object>(_imageBeanMap.size() + 1); 
				_imageDynMap.put("data", _inputStream);
				for (Object key : _imageBeanMap.keySet())
					_imageDynMap.put((String)key, _imageBeanMap.get((String)key));
					
				_renderableImages.add(_imageDynMap);
			}
			_requestDynMap.put("renderableImages", _renderableImages);
			
			_renderableImages = new ArrayList<Map<String,Object>>(_request.getImages_1().size());
			for (Image _image : _request.getImages_1()) {
				_thumb = this.imageService.retrieveThumb(_image.getId());
				if (_thumb != null)
					for (Map.Entry<String,InputStream> _thumbEntry : _thumb.entrySet())
						_inputStream = _thumbEntry.getValue();
				else 
					try {_inputStream = new FileInputStream(this.getServletContext().getRealPath("/images/broken-image.png"));}
					catch (FileNotFoundException e) {e.printStackTrace();}
					
				_imageBeanMap = new BeanMap(_image);
				_imageDynMap = new HashMap<String, Object>(_imageBeanMap.size() + 1); 
				_imageDynMap.put("data", _inputStream);
				for (Object key : _imageBeanMap.keySet())
					_imageDynMap.put((String)key, _imageBeanMap.get((String)key));
					
				_renderableImages.add(_imageDynMap);
			}
			_requestDynMap.put("renderableImages_1", _renderableImages);

			_renderableImages = new ArrayList<Map<String,Object>>(_request.getRequestattachments().size());
			for (RequestAttachment _ra : _request.getRequestattachments()) {
				
				_inputStream = this.requestService.attachmentRetrieveStream(_request.getId(), _ra.getId());
				
				_imageBeanMap = new BeanMap(_ra);
				_imageDynMap = new HashMap<String, Object>(_imageBeanMap.size() + 1); 
				_imageDynMap.put("data", _inputStream);
				for (Object key : _imageBeanMap.keySet())
					_imageDynMap.put((String)key, _imageBeanMap.get((String)key));
					
				_renderableImages.add(_imageDynMap);
			}
			_requestDynMap.put("renderableImages_attached", _renderableImages);
			
			for (Object key : _requestBeanMap.keySet())
				_requestDynMap.put((String)key, _requestBeanMap.get((String)key));

			_requestsMapCollection.add(_requestDynMap);
		}
		
		return super.convertReportData(new JRMapCollectionDataSource(_requestsMapCollection));
		
	}

}
